/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.codeminer.parts.server.actions;

import org.codeminer.parts.constants.MessageConstants;
import org.codeminer.parts.server.permissions.Permission;
import org.codeminer.parts.server.users.LoginException;
import org.codeminer.parts.server.users.NoSessionException;
import org.codeminer.parts.server.users.User;
import org.codeminer.parts.server.users.UserManager;

import java.util.EnumSet;
import java.util.Set;
import java.util.UUID;

public class Login implements Action {

    private final String VERSION = "3";
    private ActionContext context;
    private final Set<Permission> permissions = EnumSet.noneOf(Permission.class);
    private final String userName;
    private final String passHash;

    public Login(ActionContext context, String userName, String passHash) {
        this.context = context;

        this.userName = userName;
        this.passHash = passHash;

    }

    @Override
    public String getVersion() {
        return this.VERSION;
    }

    @Override
    public Set<Permission> requiredPermissions() {
        return this.permissions;
    }

    @Override
    public Response execute() {
        try {
            User loggedInUser = context.getApplicationPart(UserManager.class).login(userName, passHash);
            UUID sessionId = context.getApplicationPart(UserManager.class).getSessionId(loggedInUser);
            return createOK(sessionId);
        } catch (NoSessionException ex) {
            return createError(MessageConstants.ERROR_NO_SESSION, MessageConstants.ERROR_NO_SESSION_MESSAGE);
        } catch (LoginException ex) {
            return createError(MessageConstants.ERROR_LOGIN_SESSION, MessageConstants.ERROR_LOGIN_SESSION_MESSAGE);
        }
    }

    public String getUserName() {
        return this.userName;
    }

    public String getPassHash() {
        return this.passHash;
    }

    private Response createOK(UUID sessionId) {
        Response response = new Response("OK");
        response.addProperty("sessionId", sessionId);
        return response;
    }

    private Response createError(String messageKey, String message) {
        Response response = new Response("Error");
        response.addProperty("message.key", messageKey);
        response.addProperty("message", message);
        return response;
    }
}
